#include "GameData.h"
#include "../Framework/DataFile.h"
GameData* GameData::m_instance = nullptr;

GameData* GameData::getInstance()
{
	if (m_instance == nullptr)
		m_instance = new GameData();
	return m_instance;
}

GameData::GameData()
{
	
}

void GameData::initalize()
{
	readConfig();
	readEasy();
	readNormal();
	readHard();
}

void GameData::readConfig()
{
	DataResult dr = DataFile::readFile("Config.txt", 100);
    if (dr.data != nullptr && dr.length != 0)
	{
		for (int i = 1; i < dr.length; i++)
		{
			DataResult ss = DataFile::split(DataFile::getRow(dr.data, i), ";");

			if (std::string(ss.data[0]) == "Easy")
				config.NumOfEasyLevel = atoi(ss.data[1]);
			else if (std::string(ss.data[0]) == "Normal")
				config.NumOfNormalLevel = atoi(ss.data[1]);
			else if (std::string(ss.data[0]) == "Hard")
				config.NumOfHardLevel = atoi(ss.data[1]);

		}
	}
}

void GameData::readEasy()
{
	DataResult dr = DataFile::readFile("Easy.txt", 100);
	if (dr.data != nullptr && dr.length != 0)
	{
		for (int i = 1; i < dr.length; i++)
		{
			DataResult ss = DataFile::split(DataFile::getRow(dr.data, i), ";");

			LevelData level;
			level.ID = atoi(ss.data[EMathData::ID_LEVEL]);

			level.NumGhostStart = atoi(ss.data[EMathData::NUM_GHOST_START]);
			level.Time = atof(ss.data[EMathData::TIME]);

			if (std::string(ss.data[EMathData::TYPE]) == "Adjust")
				level.Type = EModeType::ADJUST;
			else
				level.Type = EModeType::SELECT;

			DataResult expressionQuestion = DataFile::split(ss.data[EMathData::QUESTION], ",");
			DataResult expressionAnswers = DataFile::split(ss.data[EMathData::ANSWER], ",");

			for (int j = 0; j < expressionQuestion.length; j++)
			{
				Expression expression;
				expression.Question = expressionQuestion.data[j];
				expression.Answer = atoi(expressionAnswers.data[j]);
				level.listExpression.push_back(expression);
			}

			easyMode[i] = level;
		}
	}
}

void GameData::readNormal()
{

}

void GameData::readHard()
{
	DataResult dr = DataFile::readFile("Hard.txt", 100);
	if (dr.data != nullptr && dr.length != 0)
	{
		for (int i = 1; i < dr.length; i++)
		{
			DataResult ss = DataFile::split(DataFile::getRow(dr.data, i), ";");

			LevelData level;
			level.ID = atoi(ss.data[EMathData::ID_LEVEL]);

			level.NumGhostStart = atoi(ss.data[EMathData::NUM_GHOST_START]);
			level.Time = atof(ss.data[EMathData::TIME]);

			if (std::string(ss.data[EMathData::TYPE]) == "Adjust")
				level.Type = EModeType::ADJUST;
			else
				level.Type = EModeType::SELECT;

			DataResult expressionQuestion = DataFile::split(ss.data[EMathData::QUESTION], ",");
			DataResult expressionAnswers = DataFile::split(ss.data[EMathData::ANSWER], ",");

			for (int j = 0; j < expressionQuestion.length; j++)
			{
				Expression expression;
				expression.Question = expressionQuestion.data[j];
				expression.Answer = atoi(expressionAnswers.data[j]);
				level.listExpression.push_back(expression);
			}

			hardMode[i] = level;
		}
	}
}